package com.sensetime.opencvdemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    float[] deepthValues = {
        1.5f, 2.3f, 1.5f, 2.3f, 1.5f, 2.3f,
                3.0f, 1.7f, 3.0f, 1.7f, 3.0f, 1.7f,
                1.2f, 2.9f, 1.2f, 2.9f, 1.2f, 2.9f,
                2.1f, 2.2f, 2.1f, 2.2f, 2.1f, 2.2f,
                3.1f, 3.1f, 3.1f, 3.1f, 3.1f, 3.1f,
                1.3f, 2.7f, 1.3f, 2.7f, 1.3f, 2.7f,
                2.0f, 1.7f, 2.0f, 1.7f, 2.0f, 1.7f,
                1.0f, 2.0f, 1.0f, 2.0f, 1.0f, 2.0f,
                0.5f, 0.6f, 0.5f, 0.6f, 0.5f, 0.6f,
                1.0f, 0.9f, 1.0f, 0.9f, 1.0f, 0.9f};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView imageView = (ImageView) findViewById(R.id.sample_text);
        final Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.aaa);
        imageView.setImageBitmap(bitmap);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView.setImageBitmap(ImageProcessUtils.blur(bitmap));
//                imageView.setImageBitmap(ImageProcessUtils.lighten(bitmap,50));
            }
        });

        findViewById(R.id.pca).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double[][] rawData = new double[][] {
                        { 40.4, 24.7, 7.2, 6.1, 8.3, 8.7, 2.442, 20.0 },
                        { 25.0, 12.7, 11.2, 11.0, 12.9, 20.2, 3.542, 9.1 },
                        { 13.2, 3.3, 3.9, 4.3, 4.4, 5.5, 0.578, 3.6 },
                        { 22.3, 6.7, 5.6, 3.7, 6.0, 7.4, 0.176, 7.3 },
                        { 34.3, 11.8, 7.1, 7.1, 8.0, 8.9, 1.726, 27.5 },
                        { 35.6, 12.5, 16.4, 16.7, 22.8, 29.3, 3.017, 26.6 },
                        { 22.0, 7.8, 9.9, 10.2, 12.6, 17.6, 0.847, 10.6 },
                        { 48.4, 13.4, 10.9, 9.9, 10.9, 13.9, 1.772, 1.772 },
                        { 40.6, 19.1, 19.8, 19.0, 29.7, 39.6, 2.449, 35.8 },
                        { 24.8, 8.0, 9.8, 8.9, 11.9, 16.2, 0.789, 13.7 },
                        { 12.5, 9.7, 4.2, 4.2, 4.6, 6.5, 0.874, 3.9 },
                        { 1.8, 0.6, 0.7, 0.7, 0.8, 1.1, 0.056, 1.0 },
                        { 32.3, 13.9, 9.4, 8.3, 9.8, 13.3, 2.126, 17.1 },
                        { 38.5, 9.1, 11.3, 9.5, 12.2, 16.4, 1.327, 11.6 },
                        { 26.2, 10.1, 5.6, 15.6, 7.7, 30.1, 0.126, 25.9 } };

                PrincipalComponentAnalysis pca = new PrincipalComponentAnalysis();

                long startTime = System.currentTimeMillis();
                // 运行PCA
                pca.buildPCA(rawData);
//                ImageProcessUtils.calculatePCA(deepthValues);
                Log.d("liuyi","pca cast time = " + (System.currentTimeMillis() - startTime));

                // 获得选择属性序号
                System.out.println("获得选择属性序号");
                int[] selected = pca.getSelected();
                for (int i = 0; i < selected.length; i++) {
                    System.out.print(selected[i] + " ");
                }
                System.out.println();
                System.out.println("======");

                System.out.println("获得特征值");
                double[] eigenValues = pca.getEigenValues();
                for (int i = 0; i < eigenValues.length; i++) {
                    System.out.print(eigenValues[i] + " ");
                }
                System.out.println();
                System.out.println("======");

                System.out.println("获得特征向量");
                double[][] eigenVectors = pca.getEigenVectors();
                for (int i = 0; i < eigenVectors.length; i++) {
                    for (int j = 0; j < eigenVectors[0].length; j++) {
                        System.out.print(eigenVectors[i][j] + " ");
                    }
                    System.out.println();
                }
                System.out.println();
                System.out.println("======");

                System.out.println("获得主特征值");
                double[] pEigenValues = pca.getPrincipalEigenValues();
                for (int i = 0; i < pEigenValues.length; i++) {
                    System.out.print(pEigenValues[i] + " ");
                }
                System.out.println();
                System.out.println("======");

                System.out.println("获得主特征向量");
                double[][] pEigenVectors = pca.getPrincipalEigenVectors();
                for (int i = 0; i < pEigenVectors.length; i++) {
                    for (int j = 0; j < pEigenVectors[0].length; j++) {
                        System.out.print(pEigenVectors[i][j] + " ");
                    }
                    System.out.println();
                }
                System.out.println();
                System.out.println("======");

                System.out.println("获得主要数据");
                double[][] pData = pca.getPrincipalData();
                for (int i = 0; i < pData.length; i++) {
                    for (int j = 0; j < pData[0].length; j++) {
                        System.out.print(pData[i][j] + " ");
                    }
                    System.out.println();
                }
                System.out.println("======");
            }
        });
    }

}
